<?php
require_once 'vendor/autoload.php';

use think\facade\Db;

// 初始化应用
$app = new \think\App();
$app->initialize();

echo "检查订单表结构...\n";

try {
    // 检查表是否存在
    $tables = Db::query("SHOW TABLES LIKE 'orders'");
    if (empty($tables)) {
        echo "错误：orders 表不存在\n";
        exit;
    }
    
    echo "orders 表存在\n";
    
    // 检查表结构
    $columns = Db::query("DESCRIBE orders");
    echo "表结构：\n";
    foreach ($columns as $column) {
        echo "- {$column['Field']}: {$column['Type']} ({$column['Null']})\n";
    }
    
    // 检查是否有sync_date字段
    $hasSyncDate = false;
    foreach ($columns as $column) {
        if ($column['Field'] === 'sync_date') {
            $hasSyncDate = true;
            break;
        }
    }
    
    if (!$hasSyncDate) {
        echo "\n缺少 sync_date 字段，正在添加...\n";
        Db::execute("ALTER TABLE orders ADD COLUMN sync_date datetime DEFAULT NULL COMMENT '同步时间'");
        echo "sync_date 字段添加成功！\n";
    } else {
        echo "\nsync_date 字段已存在\n";
    }

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

echo "完成！\n"; 